package qsl.utils;

//import java.util.HashMap;
//import java.util.Iterator;
//import java.io.ObjectOutputStream.PutField;
//import java.util.Iterator;
import java.util.*;
//import java.util.Map;
import java.util.Map.Entry;
//import java.util.regex.Pattern;
//import java.util.Map.Entry;

public class Cookie {
	public static void main(String[] args)
	{
		@SuppressWarnings("serial")
		final List<String> a = new ArrayList<String>() {{
			add("sid=aaaaa");
			add("cookietime=2592000");
			add("auth=xxxxxxxxxxxxxxxxx");
			add("loginuser=fffffffff");
			add("activationauth=wwwwwwwwww@");
		}};
		
		Cookie c = new Cookie() {{
			set(a);
		}};
		
		System.out.print(c.toString());
	}
	public Cookie() { }
	
	private Map<String, CookieEntity> cookies = new HashMap<String, CookieEntity> ();
	
//	Pattern p_path = Pattern.compile("path=([^;]*)"),
//			p_expires = Pattern.compile("expires=([^;]*)"),
//			p_domain = Pattern.compile("domain=([^;]*)");
	
	List<String> c = null;
	public void set(List<String> cookies) {
		c = cookies;
		
		for (String string : cookies) {
			String name = string.substring(0, string.indexOf('='));
			if (!this.cookies.containsKey(name)) this.cookies.put(name, new CookieEntity());
			this.cookies.get(name).setSource(string);
		}
	}
	@Override
	public String toString() {
//		String s = c == null ? "[]" : c.toString();
//		return s.substring(1, s.length() - 1);
		StringBuffer sb = new StringBuffer();

		Iterator<Entry<String, CookieEntity>> iterator = this.cookies.entrySet().iterator();
		while (iterator.hasNext()) {
			Entry<String, CookieEntity> entry = (Entry<String, CookieEntity>) iterator.next();
			sb.append(entry.getValue().getSource());
			if (iterator.hasNext()) sb.append(", ");
		}
		return sb.toString();
	}
	
	class CookieEntity {
		@Override
		public String toString() {
//			String string = "=" + this.value;
//			if (this.expires != null) string += "; expires=" + this.expires;
//			if (this.path != null) string += "; path=" + this.path;
//			if (this.domain != null) string += "; domain=" + this.domain;
			return this.source;
		}
		public CookieEntity() {}
		public CookieEntity(String source) {
			this.source = source;
		}
//		public CookieEntity setValue(String value) {
//			this.value = value;
//			return this;
//		}
//		public String getValue() {
//			return value;
//		}
//		public CookieEntity setExpires(String expires) {
//			this.expires = expires;
//			return this;
//		}
//		public String getExpires() {
//			return expires;
//		}
//		public CookieEntity setPath(String path) {
//			this.path = path;
//			return this;
//		}
//		public String getPath() {
//			return path;
//		}
//		public CookieEntity setDomain(String domain) {
//			this.domain = domain;
//			return this;
//		}
//		public String getDomain() {
//			return domain;
//		}
		public void setSource(String source) {
			this.source = source;
		}
		public String getSource() {
			return source;
		}
//		private String value = null;
//		private String expires = null;
//		private String path = null;
//		private String domain = null;
		private String source = "";
	}
}
